package com.qf.jie.service.impl;

import com.qf.jie.dao.CartDao;
import com.qf.jie.dao.CommodityDao;
import com.qf.jie.dao.impl.CartDaoImpl;
import com.qf.jie.dao.impl.CommentDaoImpl;
import com.qf.jie.dao.impl.CommodityDaoImpl;
import com.qf.jie.entity.bo.CartAndCommodityBo;
import com.qf.jie.entity.po.Cart;
import com.qf.jie.entity.po.Commodity;
import com.qf.jie.entity.po.User;
import com.qf.jie.entity.vo.RespBean;
import com.qf.jie.service.CartService;
import org.omg.PortableInterceptor.INACTIVE;

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

public class CartServiceImpl implements CartService {
    private CartDao cartDao = new CartDaoImpl();
    private CommodityDao commodityDao = new CommodityDaoImpl();

    @Override
    public RespBean addCart(HttpServletRequest req, String strCommodity_id) {
        try {
            int commodity_id = Integer.parseInt(strCommodity_id);
            User user = (User) req.getSession().getAttribute("user");
            int num = cartDao.addBean(new Cart(0, user.getId(), commodity_id));
            if (num == 0){
                return RespBean.respError("添加失败，未知错误");
            }
            return RespBean.respSuccess("添加成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.respError("参数异常： commodity_id=" + strCommodity_id);
        }
    }

    @Override
    public RespBean deletetById(String strId) {
        try{
            int id = Integer.parseInt(strId);
            int num = cartDao.deleteById(id);
            if (num == 0){
                return RespBean.respError("删除失败，未知错误");
            }
            return RespBean.respSuccess("删除成功");
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.respError("参数异常： id=" + strId);
        }
    }

    @Override
    public RespBean getListByUser_id(String stUser_id) {
        try{
            int user_id = Integer.parseInt(stUser_id);
            List<Cart> list = cartDao.getListByUser_id(user_id);
            List<CartAndCommodityBo> cartBos = new ArrayList<>();
            //System.out.println(list);
            if (list.size() == 0){
                return RespBean.respError("未获取到任何信息");
            }
            for (Cart cart:list){
                int com_id = cart.getCommodity_id();
                List<Commodity> beans = commodityDao.getBeanById(com_id);
                if (beans == null || beans.size() == 0){
                    continue;
                }
                cartBos.add(new CartAndCommodityBo(cart.getId(), cart.getUser_id(), cart.getCommodity_id(),beans.get(0).getName(),beans.get(0).getPrice()));
            }
            return RespBean.respSuccess(cartBos);
        }catch (Exception e){
            e.printStackTrace();
            return RespBean.respError("参数异常： user_id=" + stUser_id);
        }
    }

    @Override
    public RespBean login(HttpServletRequest request, String name, String password) {
        return null;
    }

    @Override
    public RespBean register(Cart cart) {
        return null;
    }

    @Override
    public RespBean getPage(Object currentPage, Object len) {
        return null;
    }
}
